package com.fengcheng.controller;

import com.fengcheng.service.UserService;
import com.fengcheng.util.HdfsUtil;
import com.fengcheng.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Controller
@ResponseBody
public class LoginController {

    @Autowired
     private UserService userService;

    /*
     * @author 枫城
     * @createTime 2022/7/2 16:55
     * @desc 登陆认证
     */
    @RequestMapping("/login")
    public Map<String, String> login(
            @RequestBody Map<String, String> data) {

        final Map<String, String> resultMap = new HashMap<>();
        final Map<String, String> tokenMap = new HashMap<>();

        // 获取当前用户
         Subject subject = SecurityUtils.getSubject();
        // 封装用户数据
        UsernamePasswordToken userInfo = new UsernamePasswordToken(data.get("name"), data.get("password"));
         log.info("信息：{}", data.get("name"));
        // 执行登陆方法， 可以捕获登陆异常
        try {
            subject.login(userInfo); // 没有异常则表示登陆成功
            tokenMap.put("userName",data.get("name"));
            tokenMap.put("password",data.get("password"));
            String token = JWTUtil.getToken(tokenMap);
            resultMap.put("token",token);
            resultMap.put("state", "true");

            return resultMap;
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            // 捕获用户名错误 和密码错误
            resultMap.put("state","flase");
            return resultMap;
        }

    }

    /*
     * @author 枫城
     * @createTime 2022/6/30 15:58
     * @desc 新增用户
     */
    @RequestMapping("/addUser")
    public Map<String, String> newUser(
//            Map<String, String> userInfo
            @RequestParam("name") String name,
            @RequestParam("password") String password
    ) {
         Map<String, String> result = new HashMap<>();
        log.info("信息：{}",name);
        if (name.equals("") || password.equals("")) {
            result.put("state", "false");

            result.put("info", "注册失败，信息不能为空！");
            return result;
        }
        if (userService.queryByName(name) != null) {
            log.info("注册失败，该昵称已被占用！");
            result.put("state", "false");
            result.put("info", "注册失败，该昵称已被占用！");
            return result;

        }
        try {
            userService.InsertUser(name, password);
            String path = "/"+name;
            log.info("创建目录:{}",path);
            HdfsUtil.mkdir(path);
            result.put("state", "true");
            result.put("info", "注册成功！");
            return result;
        } catch (Exception e) {
            result.put("state", "false");
            result.put("info", "注册失败!");
            return result;
        }
    }

    /**
     * 删除用户
     *
     * @param userDel
     * @return
     */
    @RequestMapping("/deleteUser")
    public Boolean deleteUser(@RequestBody String userDel) {

        if (userService.queryByName(userDel).getName().equals(userDel)) {
            userService.DeleteUser(userDel);
            log.info("成功删除用户：" + userDel);
            return true;

        } else {
            log.info("删除失败，不存在用户：" + userDel);
            return false;
        }
    }
}


